package com.gurun.camera.addDevice.util;

import com.danale.sdk.platform.constant.device.OnlineType;
import com.danale.sdk.platform.constant.deviceinfo.AddedState;
import com.gurun.camera.addDevice.entity.DeviceInfoEntity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Administrator on 2016/9/14.
 */
public class DevInfoEntityCache {
    private static DevInfoEntityCache mInstance = null;
    private DevInfoEntityCache(){};
    private Map<String,DeviceInfoEntity> mCacheMap = new ConcurrentHashMap<>();

    public static final DevInfoEntityCache getInstance(){
        if(mInstance == null){
            synchronized (DevInfoEntityCache.class){
                if(mInstance == null){
                    mInstance = new DevInfoEntityCache();
                }
            }
        }
        return mInstance;
    }

    public void updateDevInfo(DeviceInfoEntity entity){
        if(entity == null){
            return;
        }
        DeviceInfoEntity devInfo = mCacheMap.get(entity.getDeviceId());
        if(devInfo != null){
            devInfo.update(entity);
        }else{
            devInfo = new DeviceInfoEntity(entity);
        }
        mCacheMap.put(entity.getDeviceId(),devInfo);
    }

    public void updateSearchDevList(List<DeviceInfoEntity> list){
        if(list == null){
            return;
        }

        for(DeviceInfoEntity entity : list){
            updateDevInfo(entity);
        }
    }

    public List<DeviceInfoEntity> getDeviceInfoEntityList(){
        List<DeviceInfoEntity> list = new ArrayList<>(mCacheMap.values());
        return list;
    }

    public DeviceInfoEntity getDeviceInfoEntity(String deviceId){
        return mCacheMap.get(deviceId);
    }

    public void sortList(List<DeviceInfoEntity> list){
        Collections.sort(list, new Comparator<DeviceInfoEntity>() {
            @Override
            public int compare(DeviceInfoEntity lhs, DeviceInfoEntity rhs) {
                AddedState laddState = lhs.getAddedState();
                AddedState raddState = rhs.getAddedState();
                if(laddState.getNum() < raddState.getNum()){
                    return -1;
                }else if(laddState.getNum() > raddState.getNum()){
                    return 1;
                }else{
                    OnlineType lonlineType = lhs.getOnlineType();
                    OnlineType ronlineType = rhs.getOnlineType();
                    if(lonlineType.getNum() > ronlineType.getNum()){
                        return  -1;
                    }else if(lonlineType.getNum() < ronlineType.getNum()){
                        return 1;
                    }else {
                        String ldeviceId = lhs.getDeviceId().toLowerCase();
                        String rdeviceId = rhs.getDeviceId().toLowerCase();
                        int len = Math.min(ldeviceId.length(),rdeviceId.length());
                        for(int i = 0; i < len; i ++ ){
                            int result = ldeviceId.charAt(i) - rdeviceId.charAt(i);
                            if(result != 0){
                                return result;
                            }
                        }
                        return ldeviceId.length() - rdeviceId.length();
                    }
                }
            }
        });
    }


}
